package com.fengye.validation.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;


/**
 * @author haiqing.gao
 * @since 2023-12-06 13:08
 */


@Configuration
public class CommonConfiguration {

    @Autowired
    DatabaseMessageSource messageSource;

    @Bean
    public LocalValidatorFactoryBean validator() {
        LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
        // 设置快速失败模式
        factoryBean.getValidationPropertyMap().put("hibernate.validator.fail_fast", "true");
        factoryBean.setValidationMessageSource(messageSource);
        CustomMessageInterpolator messageInterpolator = new CustomMessageInterpolator(messageSource);
        factoryBean.setMessageInterpolator(messageInterpolator);
        return factoryBean;
    }
}
